package gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;

public class VNuevaPublicidad extends JPanel {
	private JTextField textField;
	private JButton btnAceptar;
	JLabel lblNewLabel;

	/**
	 * Create the panel.
	 */
	public VNuevaPublicidad() {
		setLayout(new BorderLayout(0, 0));
		
		JLabel lblReemplazarPublicidadExistente = new JLabel("Establecer Nueva Publicidad");
		lblReemplazarPublicidadExistente.setFont(new Font("Segoe UI", Font.PLAIN, 18));
		add(lblReemplazarPublicidadExistente, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(47, 6, 116, 22);
		textField.setEditable(false);
		panel.add(textField);
		textField.setColumns(10);
		
		btnAceptar = new JButton("Enviar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lblNewLabel.setText(textField.getText()+ " subido con exito");
			}
		});
		btnAceptar.setBounds(98, 41, 65, 25);
		btnAceptar.setEnabled(false);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(170, 41, 79, 25);
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cerrar();
			}

		});
		
		JButton btnExaminar = new JButton("Examinar");
		btnExaminar.setBounds(168, 5, 81, 25);
		btnExaminar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				JFileChooser chooser = new JFileChooser();
				chooser.setCurrentDirectory(new java.io.File("."));
				chooser.setDialogTitle("Seleccione una imagen");
				if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
					textField.setText(chooser.getSelectedFile().getName());
					copy(chooser.getSelectedFile());
					btnAceptar.setEnabled(true);
				}
			}

		});
		panel.add(btnExaminar);
		
		panel.add(btnAceptar);
		
		panel.add(btnCancelar);
		
		lblNewLabel = new JLabel("");
		add(lblNewLabel, BorderLayout.SOUTH);

	}

	private void cerrar() {
		this.setVisible(false);
		
	} 
	
	private void copy(File file) {
		try {
			File copia = new File("C:\\USERS\\KILO\\DESKTOP\\PRUEBA\\"+file.getName());
			InputStream in = new FileInputStream(file);
			OutputStream out = new FileOutputStream(copia);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0){
					out.write(buf, 0, len);
			}
			in.close();
			out.close();
			//System.out.println("ME COPIO");
		} catch(FileNotFoundException ex) {
			System.out.println(ex.getMessage());
			//System.exit(0);
		} catch(IOException e) {
			System.out.println(e.getMessage());  
		}
	}
}
